#include "stdafx.h"

#include "Language.h"

#include <fstream>
#include <list>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

CLanguage::CLanguage(CString file)
{
	m_FileNames.push_back(file);
	InitTypes();
}

CLanguage::CLanguage()
{
	InitTypes();
}

CLanguage::~CLanguage()
{
}

CProgram CLanguage::getProgram()
{
	return m_Program;
}

bool CLanguage::Parse()
{
	bool rtn = true;
	/*if(ParseParen() == false)
		AfxMessageBox("Failed");*/
	return rtn;
}

list<CString> CLanguage::SplitString(CString str)
{
	list<CString> ret;
	CString resToken;
	int curPos= 0;

	resToken= str.Tokenize(_T(" "),curPos);
	while (resToken != "")
	{
		ret.push_back(resToken);
		resToken= str.Tokenize(_T(" "),curPos);
	}
	return ret;
}


// open a new file
bool CLanguage::AddFile(CString fileName)
{
	bool rtn = true;

	if(fileName.Find(FILE_EXTENSION) == -1)
	{
		// add better error handling stuff
		CString tmp;
		tmp.Format(_T("Invalid file type"));
		m_Errors.NewError(tmp, -1, fileName, ERROR_FILE);
		return false;
	}

	if(find(m_FileNames.begin(), m_FileNames.end(),fileName) == m_FileNames.end())
	{
		m_FileNames.push_back(fileName);
	}

	rtn = ReadFile(fileName);

	return rtn;
}

// Read all files into memory
bool CLanguage::ReadFiles()
{
	CString fileName;
	bool rtn = true;

	for(list<CString>::iterator itr = m_FileNames.begin(); itr != m_FileNames.end(); itr++)
	{
		fileName = *itr;

		rtn = ReadFile(fileName);
	}

	return rtn;
}

bool CLanguage::ParseParen()
{
	bool rtn = true;
	CString fileName;

	for(list<CString>::iterator itr = m_FileNames.begin(); itr != m_FileNames.end(); itr++)
	{
		fileName = *itr;

		if(ParseParenFile(fileName) == false)
			rtn = false;
	}

	return rtn;
}

bool CLanguage::InsideFunction(CString line)
{
	static bool rtn;

	if(line.Find('{') != -1)
		rtn = true;
	else if(line.Find('}') != -1)
		rtn = false;

	return rtn;
}

list<CString> CLanguage::getFileNames()
{
	return m_FileNames;
}

map<CString, list<CString>> CLanguage::getFileLines()
{
	return m_Lines;
}

CError CLanguage::getErrors()
{
	return m_Errors;
}

void CLanguage::AddFileName(CString fileName)
{
	m_FileNames.push_back(fileName);
}